//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ehl.capture.tool;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import org.apache.log4j.Logger;

public class OneJVMLocker {
    private static Logger log = Logger.getLogger(OneJVMLocker.class);

    public OneJVMLocker() {
    }

    public static synchronized boolean lockInstance(final String lockFile) {
        try {
            final File file = new File(lockFile);
            final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            final FileLock fileLock = randomAccessFile.getChannel().tryLock();
            if (fileLock != null) {
                Runtime.getRuntime().addShutdownHook(new Thread() {
                    public void run() {
                        try {
                            fileLock.release();
                            randomAccessFile.close();
                            file.delete();
                        } catch (Exception var2) {
                            OneJVMLocker.log.error("Unable to remove lock file: " + lockFile, var2);
                        }

                    }
                });
                return true;
            }
        } catch (Exception var4) {
            log.error("Unable to create and/or lock file: " + lockFile, var4);
        }

        return false;
    }
}
